package com.aistar.controller;

import com.aistar.pojo.Attention;
import com.aistar.pojo.Product;
import com.aistar.pojo.ProductAttentionVo;
import com.aistar.pojo.ProductImages;
import com.aistar.service.AttentionService;
import com.aistar.service.ProductImagesService;
import com.aistar.service.ProductService;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/attention")
public class AttentionController {

    @Autowired
    private ProductImagesService productImagesService;

    @Autowired
    private AttentionService attentionService;

    @Autowired
    private ProductService productService;

    @GetMapping("/add/{customerId}/{productId}")
    @ResponseBody
    public ServerResponse AddAttention(@PathVariable("customerId") Integer customerId, @PathVariable("productId") Integer productId){
        //先去service层查询有没有该条记录
        ServerResponse serverResponse = attentionService.getByCustomerIdProductId(customerId, productId);
        if (serverResponse.getStatus()==0){
            Attention attention = new Attention();
            //根据商品id 去查商品的图片
            ServerResponse sr = productImagesService.getByProductId(productId);
            if (sr.getStatus()==1){
                List<ProductImages> productImagesList = (List<ProductImages>) sr.getData();
                String imagesPath = productImagesList.get(0).getProductImagesImage();
                attention.setCustomerId(customerId);
                attention.setAttentionStatus(1);
                attention.setProductId(productId);
                attention.setProductImages(imagesPath);
                attention.setAttentionTime(new Date());

            }
            return attentionService.save(attention);

        }else {
            return ServerResponse.getDataFailed("您已关注过此商品，不需要重复关注");
        }


    }


    /*@GetMapping("/delete/{customerId}/{productId}")
    @ResponseBody
    public ServerResponse deleteAttention(@PathVariable("customerId") Integer customerId, @PathVariable("productId") Integer productId){
        //先根据用户id 商品id 查出这条关注信息，再将其状态修改为1
        ServerResponse serverResponse = attentionService.getByCustomerIdProductId(customerId, productId);
        if (serverResponse.getStatus()==1){
            List<Attention> attentionList = (List<Attention>) serverResponse.getData();
            Attention attention = attentionList.get(0);
            //调用方法将其状态修改为0
            ServerResponse serverResponse1 = attentionService.modifyStatus(attention);
            return serverResponse1;
        }else {
            return ServerResponse.deleteFailed();
        }

    }*/

    //删除
    @GetMapping("/delete/{attentionId}")
    @ResponseBody
    public ServerResponse deleteByAttentionId(@PathVariable("attentionId")Integer attentionId){
        ServerResponse serverResponse = attentionService.getById(attentionId);
        if (serverResponse.getStatus()==1){
            //查询出来了 有数据
            Attention attention = (Attention) serverResponse.getData();
            ServerResponse sr = attentionService.modifyStatus(attention);
            return sr;
        }else {
            return ServerResponse.deleteFailed();
        }

    }


    @GetMapping("/getAll/{customerId}")
    @ResponseBody
    public ServerResponse getAllAttention(@PathVariable("customerId")Integer customerId){
        ServerResponse serverResponse = attentionService.getAllByCustomerId(customerId);
        List<ProductAttentionVo> productAttentionVoList = new ArrayList<>();
        if (serverResponse.getStatus()==1){
            List<Attention> attentionList = (List<Attention>) serverResponse.getData();
            //循环拿到商品的id price

            for (Attention attention : attentionList){
                ProductAttentionVo productAttentionVo = new ProductAttentionVo();
              ServerResponse sr =  productService.getById(attention.getProductId());
              if (sr .getStatus() ==1){
                   Product product = (Product) sr.getData();
                   productAttentionVo.setProduct(product);
              }
              productAttentionVo.setAttention(attention);
              productAttentionVoList.add(productAttentionVo);
            }
            return ServerResponse.getDataSuccess(productAttentionVoList);
        }else {
            return ServerResponse.getDataFailed("您还未收藏任何商品");
        }

    }


}
